package com.tul.exercise.base.jmockit.mockpublicmethod;

import com.tul.exercise.base.jmockit.mockpublicmethod.itf.Demo1DAO;
import mockit.Expectations;
import mockit.Injectable;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * 《JMockit中文版入门指南》 1.4 Mock 普通公有方法
 * Demo1ServiceImpl测试类
 * mock部分方法
 *
 * @author tuliang
 */
@ContextConfiguration(classes = SpringConfig.class)
public class Demo1ServiceImplTest1 {

    //@Mocked(methods = {"getName"})
    @Injectable
    @Qualifier("demo1DAO")
    @Autowired
    Demo1DAO demo1DAO;

    @Test
    public void test1() {
        String name = "mayun";
        int age = 18;
        new Expectations(){{
            demo1DAO.getName();
            result = "mayun";

            demo1DAO.getAge();
            result = age;
        }};

        System.out.println(demo1DAO.getName());
        // ??,getName()方法没有被设置预期
        assertEquals(demo1DAO.getName(), name);

        System.out.println(demo1DAO.getAge());
        // ??
        assertEquals(demo1DAO.getAge(), age);
        System.out.println(demo1DAO.getName());

    }
}
